<template>
    <view class="chooseCouponStyle">
        <view class="couponList" :style="{ paddingBottom: isIphoneX ? 'env(safe-area-inset-bottom)' : '20rpx' }">
            <view v-for="(item, index) in historyList" :key="item.id" class="couponItem">
                <CouponItem :btnText="getCouponSuatusText(item)" :coupon="item" :isUnusable="true" />
            </view>
        </view>
    </view>
</template>

<script>
import CouponItem from "../components/couponItem/index.vue";
import utils from "@/common/utils.js";

export default {
    name: "myCouponHistoryList",
    components: {
        CouponItem,
    },
    data() {
        return {
            isIphoneX: false,
            historyList: [],
            queryParams: {
                pageNum: 1,
                pageSize: 4,
            },
            loading: false,
            noMore: false,
        };
    },
    methods: {
        async getHistoryList() {
            const { data } = await this.request.httpTokenJsonRequest(
                "/mp-api/wx/coupon/myHistory",
                this.queryParams,
                "GET",
            );
            this.historyList = data || [];
        },
        getCouponSuatusText(data) {
            if (data.useStatus === 0) {
                const endTimeStr = data.termEnd.replaceAll("-", "/");
                if (new Date(endTimeStr).getTime() < Date.now()) {
                    return "已过期";
                }
                return "未使用";
            }
            if (data.useStatus === 1) {
                return "已使用";
            }
            if (data.useStatus === 2) {
                return "已过期";
            }
            if (data.useStatus === 3) {
                return "已作废";
            }
            return "";
        },
    },
    onLoad() {
        utils.checkIphoneX().then((res) => (this.isIphoneX = res));
        this.getHistoryList();
    },
};
</script>

<style lang="scss" scoped>
.chooseCouponStyle {
    .couponList {
        flex: 1;
        overflow-y: auto;
        padding: 20rpx 30rpx 0;
        margin-bottom: 20rpx;
    }

    .couponItem:last-child {
        margin-bottom: 100rpx;
    }

    .couponFooter {
        position: fixed;
        bottom: 0;
        left: 0;
        z-index: 5;
        width: 100vw;
        padding-top: 20rpx;
        background-color: #ffffff;
    }

    .footerBtn {
        height: 44rpx;
        margin: 0 30rpx;
        border-radius: 24rpx;
        line-height: 44rpx;
        font-size: 30rpx;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #333333;
        text-align: center;
    }

    .emptyArea {
        display: flex;
        flex-direction: column;
        align-items: center;
    }
}
</style>
